package com.trkj.property_controller;

import com.trkj.entity.RoleEntity;
import com.trkj.entity.UserEntity;
import com.trkj.entity.UserRoleEntity;
import com.trkj.service.UserService;
import com.trkj.vo.AjaxResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class UserController {
    private final static String abbrPath = System.getProperty("user.dir") + "/property_houduan/property_controller/src/main/resources/static/file/";
    private String insertabbr;
    @Resource
    private UserService userService;

    @PostMapping("/upload")
    @ResponseBody
    public Object uploadFile(@RequestParam("file") MultipartFile[] multipartFiles) {
        for (MultipartFile multipartFile : multipartFiles) {
            System.out.println("我来了-----" + multipartFile.getOriginalFilename());
        }
        try {
            if (multipartFiles != null && multipartFiles.length > 0) {
                for (int i = 0; i < multipartFiles.length; i++) {
                    try {
                        //以原来的名称命名,覆盖掉旧的，这里也可以使用UUID之类的方式命名，这里就没有处理了
                        String storagePath = abbrPath + multipartFiles[i].getOriginalFilename();
                        insertabbr = abbrPath + multipartFiles[i].getOriginalFilename();
                        System.out.println("上传的文件：" + multipartFiles[i].getName() + "," + multipartFiles[i].getContentType() + "," + multipartFiles[i].getOriginalFilename()
                                + "，保存的路径为：" + storagePath);
                        //  第1种
                        multipartFiles[i].transferTo(new File(storagePath));
                        //连接数据库
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "文件上传成功";
    }

    @GetMapping("/download/{fileName}")
    public void downloadFile(@PathVariable String fileName, HttpServletResponse response) {
        // 获取真实的文件路径
        String filePath = abbrPath + fileName;
        System.out.println("++++完整路径为：" + filePath);

        try {
            // 下载文件
            // 设置响应头
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

            // 读取文件内容并写入输出流
            Files.copy(Paths.get(filePath), response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException e) {
            response.setStatus(404);
        }
    }

    //查询员工表
    @PostMapping("/UserList")
    public AjaxResponse UserList(@RequestBody UserRoleEntity userRoleEntity, int currentPage, int pageSize) {
        return AjaxResponse.success(userService.UserList(userRoleEntity, currentPage, pageSize));
    }

    //查询角色表
    @PostMapping("/RoleList")
    public AjaxResponse RoleList() {
        return AjaxResponse.success(userService.RoleList());
    }

    @PostMapping("/MaxUserId")
    public AjaxResponse MaxUserId() {
        return AjaxResponse.success(userService.MaxUserId());
    }

    //添加员工
    @PostMapping("/AddUser")
    public AjaxResponse AddUser(@RequestBody UserEntity userEntity) {
        return AjaxResponse.success(userService.AddUser(userEntity));
    }

    //根据员工id进行查询
    @PostMapping("/ChaUseridList/{id}")
    public AjaxResponse ChaUseridList(@PathVariable("id") int user_id) {
        return AjaxResponse.success(userService.ChaUseridList(user_id));
    }

    //修改员工表
    @PutMapping("/UpdUser")
    public AjaxResponse UpdUser(@RequestBody UserEntity userEntity) {
        return AjaxResponse.success(userService.UpdUser(userEntity));
    }

    //添加角色表
    @PostMapping("/AddRole")
    public AjaxResponse AddRole(RoleEntity roleEntity) {
        return AjaxResponse.success(userService.AddRole(roleEntity));
    }

    //添加员工角色表
    @PostMapping("/AddUserRole")
    public AjaxResponse AddUserRole(@RequestBody UserRoleEntity userRoleEntity) {
        return AjaxResponse.success(userService.AddUserRole(userRoleEntity));
    }

    @PutMapping("/UpdUserRole")
    public AjaxResponse UpdUserRole(@RequestBody UserRoleEntity userRoleEntity) {
        return AjaxResponse.success(userService.UpdUserRole(userRoleEntity));
    }

    @DeleteMapping("DelUserRole/{id}")
    public AjaxResponse DelUserRole(@PathVariable("id") int id) {
        return AjaxResponse.success(userService.DelUserRole(id));
    }

    @DeleteMapping("DelUser/{id}")
    public AjaxResponse DelUser(@PathVariable("id") int id) {
        return AjaxResponse.success(userService.DelUser(id));
    }

    @PostMapping("/ZhiXingRen")
    public AjaxResponse ZhiXingRen() {
        return AjaxResponse.success(userService.ZhiXingRen());
    }

}
